home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3527 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  78 lines

  1. Newsgroups: comp.lang.c
  2. Path: Dortmund.Germany.EU.net!gellbo!wolfi
  3. From: wolfi@gellbo.gellrich-gmbh.de (Actio wolfi)
  4. Subject: Re: realloc question
  5. Message-ID: <QG28B81O@gellbo.gellrich-gmbh.de>
  6. Organization: Gellrich-GmbH
  7. References: <4ehi4b$qfo@news.texas.net>
  8. Date: Mon, 29 Jan 1996 19:01:21 GMT
  9.  
  10. In <4ehi4b$qfo@news.texas.net> gcherer@millenium.texas.net (GT Cherer) writes:
  11.  
  12. >Dudes-
  13. >Whether i am on my old 286 with 1 meg, or on my 486 with 8 meg of ram, i 
  14. >get the same result when i do a realloc on about the 50th iteration.( its
  15. >on an array of pointers). but, when i compile and run the same program on 
  16. >a unix box (with beacoups more memory) it runs with no probs.
  17.  
  18. >is there a difference in realloc on a dos machine vs unix box? 
  19. >Is there some kind of limiting factor with realloc (other than the amount 
  20. >of ram), like a heap or stack size or something?? i follow the realloc 
  21. >with a calloc for a  spot to put a small structure so the two lines look 
  22. >like this, with the realloc returning a NULL:
  23. >x++;
  24. >ptr=(struct sumthin**) realloc(ptr,(x+1) * sizeof(struct sumthin));
  25. >ptr[x]=(struct sumthin*) calloc(1,sizeof(struct sumthin));
  26.  
  27. >Any ideas?? Sure preeeeeciate 'em. TIA
  28.  
  29. There should be no difference, except that your UNIX-Box has more memory and
  30. when the memory gets exhausted it starts to swap !
  31.  
  32. The problem with realloc is that realloc gives you a pointer to a memory
  33. area which is as big as you want. When there is no chance to make an
  34. already allocated memory area larger a new area is used and the contents of
  35. the previous allocated area is copied to the new area. When your program
  36. has a sequence of realloc .. malloc .. realloc .. malloc ..., then after a
  37. while you have used only a part of memory, but *NOT ONE BIG AREA* which
  38. the realloc can use to satisfy your request. So there are some solutions to
  39. avoid fragmenting of your memory.
  40.  
  41. a) Use linked lists. Linked lists need more memory for the pointer stuff
  42.    pointing to the next element, so this is a good solution for bigger
  43.    structures.
  44.    But it needs a lot of rewriting ...
  45.  
  46.  
  47. b) When you need an extra entry in your realloc'ed array do not call
  48.    realloc with only ony extra element, realloc an estimated extra number
  49.    of elements *AND* at the first time when calling malloc also allocate
  50.    an estimated extra number of elements.
  51.    You just have to add another counter counting the number of elements
  52.    allocated (I think you have a counter counting the number of elements
  53.    used).
  54.  
  55. c) Rewrite your program doing two passes. The first pass just counts the
  56.    number of elements needed, the second pass reads them in.
  57.  
  58. I prefer b) for my programs and use a macro like this:
  59.  
  60. #define BLOCK_ALLOCATE(ptr, typ, no_elements, no_allocated, increment)         \
  61.     {                                                                          \
  62.         if(no_elements >= no_allocated)                                        \
  63.         {                                                                      \
  64.             if(NULL == (ptr = no_elements ?                                    \
  65.                 (typ *)realloc((char *)ptr,                                    \
  66.                         (unsigned)(no_allocated += increment) * sizeof(typ)) : \
  67.                 (typ *)malloc(sizeof(typ) * (no_allocated = increment))))      \
  68.                 abort();                                                       \
  69.         }                                                                      \
  70.     }
  71.  
  72. Hope this helps.
  73.  
  74. Excuse my poor english ;-)
  75.  
  76. --
  77. wolfi@gellbo.gellrich-gmbh.de (Wolfgang Formann)
  78.